package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import static com.alibaba.fastjson.JSON.parseObject;
import static com.alibaba.fastjson.JSON.toJavaObject;
import static java.lang.String.valueOf;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.YunxinUrl;
import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.CodeForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.YunxinBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.AccidForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.CreateAccountForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.EditRoomForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.GetChatroomAddrForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.QueryRoomInfoForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.SendChatroomMsgForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.SetMemberRoleForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.SetSpecialRelationForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.UpdateRoomStatusForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.UserInfoForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.ChatroomBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.CodeBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.CreateAccountBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.GetChatroomAddrBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.SendChatroomMsgBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.SetMemberRoleBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.UpdateAccountBackVo;
import com.cloudbroker.bcs.platform.dav.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.dav.common.constant.IFSField;
import com.cloudbroker.bcs.platform.dav.common.util.CheckSumBuilder;
import com.cloudbroker.bcs.platform.dav.common.util.HttpUtil;

/**
 * Created by xuyn15828 on 2016/5/12.
 */
@Service
public class YunxinBizServiceImpl implements YunxinBizService {
    private Map<String,String> headerMap;

    public YunxinBizServiceImpl() {
        String curTime = valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(ConfigValues.getYunxinAppSecret(),
                ConfigValues.getYunxinNonce(), curTime);// 参考 计算CheckSum的java代码
        // 设置请求的header
        headerMap = new HashMap<>();
        headerMap.put("AppKey" , ConfigValues.getYunxinAppkey());
        headerMap.put("Nonce" , ConfigValues.getYunxinNonce());
        headerMap.put("CurTime", curTime);
        headerMap.put("CheckSum", checkSum);
        headerMap.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    }

    @Override
    public CreateAccountBackVo createAccount(CreateAccountForm form) {
        CreateAccountBackVo result = new CreateAccountBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.CREATE_ACCOUNT, form , headerMap));
        if( null != json && json.containsKey(IFSField.INFO) ) {
            result = toJavaObject((JSON) json.get("info"), CreateAccountBackVo.class);
        }
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public CodeBackVo updateAccount(CreateAccountForm form) {
        CodeBackVo result = new CodeBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.UPDATE_ACCOUNT, form , headerMap));
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public UpdateAccountBackVo updateAndGetAccount(CodeForm form) {
        UpdateAccountBackVo result = new UpdateAccountBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.UPDATE_AND_GET, form , headerMap));
        if(  null != json && json.containsKey(IFSField.INFO) ) {
            result = toJavaObject(json, UpdateAccountBackVo.class);
        }
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public CodeBackVo closureAccount(AccidForm form) {
        CodeBackVo result = new CodeBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.CLOSURE_ACCOUNT, form , headerMap));
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public CodeBackVo unbanAccount(AccidForm form) {
        CodeBackVo result = new CodeBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.UNBAN_ACCOUNT, form , headerMap));
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public CodeBackVo updateUserInfo(UserInfoForm form) {
        CodeBackVo result = new CodeBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.UPDATE_USER_INFO, form , headerMap));
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public ChatroomBackVo createRoom(EditRoomForm form) {
        ChatroomBackVo result = new ChatroomBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.CREATE_CHATROOM, form , headerMap));
        if(  null != json && json.containsKey(IFSField.CHATROOM) ) {
            result = toJavaObject((JSON) json.get(IFSField.CHATROOM), ChatroomBackVo.class);
        }
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public ChatroomBackVo queryRoomInfo(QueryRoomInfoForm form) {
        ChatroomBackVo result = new ChatroomBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.QUERY_ROOM_INFO , form , headerMap));
        if(  null != json && json.containsKey(IFSField.CHATROOM) ) {
            result = toJavaObject((JSON) json.get(IFSField.CHATROOM), ChatroomBackVo.class);
        }
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public ChatroomBackVo updateRoomInfo(EditRoomForm form) {
        ChatroomBackVo result = new ChatroomBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.UPDATE_ROOM_INFO , form , headerMap));
        if(  null != json && json.containsKey(IFSField.CHATROOM) ) {
            result = toJavaObject((JSON) json.get(IFSField.CHATROOM), ChatroomBackVo.class);
        }
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public ChatroomBackVo updateRoomStatus(UpdateRoomStatusForm form) {
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.UPDATE_ROOM_STATUS , form , headerMap));
        ChatroomBackVo result = toJavaObject(json, ChatroomBackVo.class);
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public SetMemberRoleBackVo setMemberRole(SetMemberRoleForm form) {
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.SET_MEMBER_ROLE , form , headerMap));
        SetMemberRoleBackVo result = toJavaObject(json, SetMemberRoleBackVo.class);
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public GetChatroomAddrBackVo getChatroomAddr(GetChatroomAddrForm form) {
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.GET_CHATROOM_ADDR , form , headerMap));
        GetChatroomAddrBackVo result = toJavaObject(json, GetChatroomAddrBackVo.class);
        return result;
    }

    @Override
    public SendChatroomMsgBackVo sendChatroomMsg(SendChatroomMsgForm form) {
        SendChatroomMsgBackVo result = new SendChatroomMsgBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.SEND_MESSAGE , form , headerMap));
        if(  null != json && json.containsKey(IFSField.DESC) ) {
            result = toJavaObject((JSON) json.get(IFSField.DESC), SendChatroomMsgBackVo.class);
        }
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }

    @Override
    public CodeBackVo setSpecialRelation(SetSpecialRelationForm form) {
        CodeBackVo result = new CodeBackVo();
        JSONObject json = parseObject(HttpUtil.doPost(YunxinUrl.SET_SPECIAL_RELATION, form , headerMap));
        result.setCode(ValueUtil.getString(json.get(IFSField.CODE)));
        return result;
    }
}
